home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / ear / mui23dev.lha / MUI / Developer / C / Examples / Class2.c < prev    next >
C/C++ Source or Header  |  1994-12-23  |  7KB  |  285 lines

  1. #include "demo.h"
  2.  
  3.  
  4. /***************************************************************************/
  5. /* Here is the beginning of our simple new class...                        */
  6. /***************************************************************************/
  7.  
  8. /*
  9. ** This class is the same as within Class1.c except that it features
  10. ** a pen attribute.
  11. */
  12.  
  13. struct MyData
  14. {
  15.     LONG pen;
  16. };
  17.  
  18.  
  19. #define MYATTR_PEN 0x8022   /* tag value for the new attribute.            */
  20.                             /* use 0x8000 | <yourregnumber> as upper word! */
  21.  
  22.  
  23. SAVEDS ULONG mNew(struct IClass *cl,Object *obj,Msg msg)
  24. {
  25.     struct MyData *data;
  26.  
  27.     if (!(obj = (Object *)DoSuperMethodA(cl,obj,msg)))
  28.         return(0);
  29.  
  30.     data = INST_DATA(cl,obj);
  31.  
  32.     /* parse initial taglist */
  33.  
  34.     data->pen = GetTagData(MYATTR_PEN, TEXTPEN, ((struct opSet *)msg)->ops_AttrList);
  35.  
  36.     return((ULONG)obj);
  37. }
  38.  
  39.  
  40.  
  41. SAVEDS ULONG mDispose(struct IClass *cl,Object *obj,Msg msg)
  42. {
  43.     /* OM_NEW didnt allocates something, just do nothing here... */
  44.     return(DoSuperMethodA(cl,obj,msg));
  45. }
  46.  
  47.  
  48. /*
  49. ** OM_SET method, we need to see if someone changed the pen attribute.
  50. */
  51.  
  52. SAVEDS ULONG mSet(struct IClass *cl,Object *obj,Msg msg)
  53. {
  54.     struct MyData *data = INST_DATA(cl,obj);
  55.     struct TagItem *tags,*tag;
  56.  
  57.     for (tags=((struct opSet *)msg)->ops_AttrList;tag=NextTagItem(&tags);)
  58.     {
  59.         switch (tag->ti_Tag)
  60.         {
  61.             case MYATTR_PEN:
  62.                 data->pen = tag->ti_Data;         /* set the new value */
  63.                 MUI_Redraw(obj,MADF_DRAWOBJECT);  /* redraw ourselves completely */
  64.                 break;
  65.         }
  66.     }
  67.  
  68.     return(DoSuperMethodA(cl,obj,msg));
  69. }
  70.  
  71.  
  72. /*
  73. ** OM_GET method, see if someone wants to read the color.
  74. */
  75.  
  76. static ULONG mGet(struct IClass *cl,Object *obj,Msg msg)
  77. {
  78.     struct MyData *data = INST_DATA(cl,obj);
  79.     ULONG *store = ((struct opGet *)msg)->opg_Storage;
  80.  
  81.     switch (((struct opGet *)msg)->opg_AttrID)
  82.     {
  83.         case MYATTR_PEN: *store = data->pen; return(TRUE);
  84.     }
  85.  
  86.     return(DoSuperMethodA(cl,obj,msg));
  87. }
  88.  
  89.  
  90. SAVEDS ULONG mAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  91. {
  92.     /*
  93.     ** let our superclass first fill in what it thinks about sizes.
  94.     ** this will e.g. add the size of frame and inner spacing.
  95.     */
  96.  
  97.     DoSuperMethodA(cl,obj,msg);
  98.  
  99.     /*
  100.     ** now add the values specific to our object. note that we
  101.     ** indeed need to *add* these values, not just set them!
  102.     */
  103.  
  104.     msg->MinMaxInfo->MinWidth  += 100;
  105.     msg->MinMaxInfo->DefWidth  += 120;
  106.     msg->MinMaxInfo->MaxWidth  += 500;
  107.  
  108.     msg->MinMaxInfo->MinHeight += 40;
  109.     msg->MinMaxInfo->DefHeight += 90;
  110.     msg->MinMaxInfo->MaxHeight += 300;
  111.  
  112.     return(0);
  113. }
  114.  
  115.  
  116. /*
  117. ** Draw method is called whenever MUI feels we should render
  118. ** our object. This usually happens after layout is finished
  119. ** or when we need to refresh in a simplerefresh window.
  120. ** Note: You may only render within the rectangle
  121. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  122. */
  123.  
  124. SAVEDS ULONG mDraw(struct IClass *cl,Object *obj,struct MUIP_Draw *msg)
  125. {
  126.     struct MyData *data = INST_DATA(cl,obj);
  127.     int i;
  128.  
  129.     /*
  130.     ** let our superclass draw itself first, area class would
  131.     ** e.g. draw the frame and clear the whole region. What
  132.     ** it does exactly depends on msg->flags.
  133.     */
  134.  
  135.     DoSuperMethodA(cl,obj,msg);
  136.  
  137.     /*
  138.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  139.     ** MUI just wanted to update the frame or something like that.
  140.     */
  141.  
  142.     if (!(msg->flags & MADF_DRAWOBJECT))
  143.         return(0);
  144.  
  145.     /*
  146.     ** ok, everything ready to render...
  147.     */
  148.  
  149.     SetAPen(_rp(obj),_dri(obj)->dri_Pens[data->pen]);
  150.  
  151.     for (i=_mleft(obj);i<=_mright(obj);i+=5)
  152.     {
  153.         Move(_rp(obj),_mleft(obj),_mbottom(obj));
  154.         Draw(_rp(obj),i,_mtop(obj));
  155.         Move(_rp(obj),_mright(obj),_mbottom(obj));
  156.         Draw(_rp(obj),i,_mtop(obj));
  157.     }
  158.  
  159.     return(0);
  160. }
  161.  
  162.  
  163. /*
  164. ** Here comes the dispatcher for our custom class. We only need to
  165. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  166. ** Unknown/unused methods are passed to the superclass immediately.
  167. */
  168.  
  169. SAVEDS ASM ULONG MyDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  170. {
  171.     switch (msg->MethodID)
  172.     {
  173.         case OM_NEW        : return(mNew      (cl,obj,(APTR)msg));
  174.         case OM_DISPOSE    : return(mDispose  (cl,obj,(APTR)msg));
  175.         case OM_SET        : return(mSet      (cl,obj,(APTR)msg));
  176.         case OM_GET        : return(mGet      (cl,obj,(APTR)msg));
  177.         case MUIM_AskMinMax: return(mAskMinMax(cl,obj,(APTR)msg));
  178.         case MUIM_Draw     : return(mDraw     (cl,obj,(APTR)msg));
  179.     }
  180.  
  181.     return(DoSuperMethodA(cl,obj,msg));
  182. }
  183.  
  184.  
  185.  
  186. /***************************************************************************/
  187. /* Thats all there is about it. Now lets see how things are used...        */
  188. /***************************************************************************/
  189.  
  190. int main(int argc,char *argv[])
  191. {
  192.     APTR app,window,MyObj,cycle;
  193.     struct MUI_CustomClass *mcc;
  194.     ULONG signals;
  195.     BOOL running = TRUE;
  196.     static char *penarray[] =
  197.     {
  198.         "Detailpen",
  199.         "Blockpen",
  200.         "Textpen",
  201.         "Shinepen",
  202.         "Shadowpen",
  203.         "Fillpen",
  204.         NULL
  205.     };
  206.  
  207.     init();
  208.  
  209.     /* Create the new custom class with a call to MUI_CreateCustomClass(). */
  210.     /* Caution: This function returns not a struct IClass, but a           */
  211.     /* struct MUI_CustomClass which contains a struct IClass to be         */
  212.     /* used with NewObject() calls.                                        */
  213.     /* Note well: MUI creates the dispatcher hook for you, you may         */
  214.     /* *not* use its h_Data field! If you need custom data, use the        */
  215.     /* cl_UserData of the IClass structure!                                */
  216.  
  217.     if (!(mcc = MUI_CreateCustomClass(NULL,MUIC_Area,NULL,sizeof(struct MyData),MyDispatcher)))
  218.         fail(NULL,"Could not create custom class.");
  219.  
  220.     app = ApplicationObject,
  221.         MUIA_Application_Title      , "Class2",
  222.         MUIA_Application_Version    , "$VER: Class2 1.0 (01.12.93)",
  223.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  224.         MUIA_Application_Author     , "Stefan Stuntz",
  225.         MUIA_Application_Description, "Demonstrate the use of custom classes.",
  226.         MUIA_Application_Base       , "CLASS2",
  227.  
  228.         SubWindow, window = WindowObject,
  229.             MUIA_Window_Title, "Another Custom Class",
  230.             MUIA_Window_ID   , MAKE_ID('C','L','S','2'),
  231.             WindowContents, VGroup,
  232.  
  233.                 Child, cycle = KeyCycle(penarray,' '),
  234.  
  235.                 Child, MyObj = NewObject(mcc->mcc_Class,NULL,
  236.                     TextFrame,
  237.                     MUIA_Background, MUII_BACKGROUND,
  238.                     TAG_DONE),
  239.  
  240.                 End,
  241.  
  242.             End,
  243.         End;
  244.  
  245.     if (!app)
  246.         fail(app,"Failed to create Application.");
  247.  
  248.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  249.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  250.  
  251.     DoMethod(cycle,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
  252.         MyObj,3,MUIM_Set,MYATTR_PEN,MUIV_TriggerValue);
  253.  
  254.     set(cycle,MUIA_Cycle_Active,TEXTPEN);
  255.  
  256. /*
  257. ** Input loop...
  258. */
  259.  
  260.     set(window,MUIA_Window_Open,TRUE);
  261.  
  262.     while (running)
  263.     {
  264.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  265.         {
  266.             case MUIV_Application_ReturnID_Quit:
  267.                 running = FALSE;
  268.                 break;
  269.         }
  270.  
  271.         if (running && signals) Wait(signals);
  272.     }
  273.  
  274.     set(window,MUIA_Window_Open,FALSE);
  275.  
  276.  
  277. /*
  278. ** Shut down...
  279. */
  280.  
  281.     MUI_DisposeObject(app);     /* dispose all objects. */
  282.     MUI_DeleteCustomClass(mcc); /* delete the custom class. */
  283.     fail(NULL,NULL);            /* exit, app is already disposed. */
  284. }
  285.